package Offer2017;

/**
 * 替换空格
 * Created by 雷文康 on 2017/12/27.
 */
public class Test04 {
    public static void main(String[] args) {
        System.out.println(replaceBlank("we are not happy"));
    }

    public static String replaceBlank(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        char[] oldChar = str.toCharArray();
        int blankNum = 0;
        for (int i = 0; i < oldChar.length; i++){
            if (oldChar[i] == ' '){
                blankNum++;
            }
        }
        char[] newChar = new char[oldChar.length + 2 * blankNum];
        int j = 0;
        for (int i = 0; i < oldChar.length; i++){
            if (oldChar[i] == ' '){
                newChar[j++] = '2';
                newChar[j++] = '0';
                newChar[j++] = '%';
            } else {
                newChar[j++] = oldChar[i];
            }
        }
        return new String(newChar);

    }
}
